"""
Case Type   : Operator
Case Name   : 修改未定义的操作符，合理报错
Create At   : 2024.10.8
Owner       : wang-tianjing1
Description :
    1、设置参数support_extended_features为on
    2、修改未定义的操作符
    3、清理环境
Expect      :
    1、成功
    2、失败
    3、成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node


class OpreatorTest(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.common = Common()
        self.sh_primary = CommonSH('dbuser')
        self.user_node = Node('dbuser')
        self.constant = Constant()

    def test_dolphin(self):
        text = '----step1:设置参数support_extended_features为on；expect：创建成功----'
        self.log.info(text)
        sql_cmd = f'alter system set support_extended_features to on;'
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn('ALTER SYSTEM SET', sql_res, '执行失败' + text)
        self.log.info('-----重启数据库------')
        msg = self.sh_primary.restart_db_cluster()
        self.log.info(msg)
        status = self.sh_primary.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status,
                        '执行失败:' + text)

        text = '----step2:修改操作符时缺省的操作数填写为none;expect:修改失败----'
        self.log.info(text)
        sql_cmd1 = f'alter operator ##### (int, int) owner to user;'
        self.log.info(sql_cmd1)
        sql_res1 = self.sh_primary.execut_db_sql(sql_cmd1)
        self.log.info(sql_res1)
        self.assertIn('''ERROR:  syntax error at or near "user"''', sql_res1, '执行失败' + text)

    def tearDown(self):
        text = '--step3:清理环境;expect:成功--'
        self.log.info(text)
        text = '----恢复参数----'
        self.log.info(text)
        sql_cmd = f'alter system set support_extended_features to off;'
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn('ALTER SYSTEM SET', sql_res, '执行失败' + text)
        self.log.info('-----重启数据库------')
        msg = self.sh_primary.restart_db_cluster()
        self.log.info(msg)
        status = self.sh_primary.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status,
                        '执行失败:' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
